package com.sqpoll.android;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.sqpoll.android.group.Group;
import com.sqpoll.android.group.GroupListAdapter;
import com.sqpoll.android.group.GroupPollListActivity;
import com.sqpoll.android.poll.PollCreateActivity;
import com.sqpoll.android.settings.SettingsActivity;

/**
 * The fragment for group list.
 * 
 * @author Younggue Bae
 */
public class GroupListFragment extends SherlockFragment {

	public static final String TAG = "GroupPollFragment";

	ListView mListView;
	GroupListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_group_list, container, false);

		mListView = (ListView) view.findViewById(R.id.list_view_group);

		mAdapter = new GroupListAdapter(getActivity(), R.layout.list_item_group);

		Resources res = getResources();
		
		Group group1 = new Group();
		group1.setGroupImage(res.getDrawable(R.drawable.ic_list_users));
		group1.setGroupName("Family");
		group1.setLatestPoll("Which one do you like?");
		group1.setNewPollCount(5);
		
		Group group2 = new Group();
		group2.setGroupImage(res.getDrawable(R.drawable.ic_list_users));
		group2.setGroupName("School");
		group2.setLatestPoll("");
		group2.setNewPollCount(0);
		
		Group group3 = new Group();
		group3.setGroupImage(res.getDrawable(R.drawable.ic_list_users));
		group3.setGroupName("Work");
		group3.setLatestPoll("");
		group3.setNewPollCount(0);
		
		Group group4 = new Group();
		group4.setGroupImage(res.getDrawable(R.drawable.ic_list_users));
		group4.setGroupName("Internet community");
		group4.setLatestPoll("What do you think...");
		group4.setNewPollCount(2);
		
		Group group5 = new Group();
		group5.setGroupImage(getResources().getDrawable(R.drawable.ic_list_users));
		group5.setGroupName("Roboto");
		group5.setLatestPoll("Which one do you like?");
		group5.setNewPollCount(5);
		
		mAdapter.addItem(group1);
		mAdapter.addItem(group2);
		mAdapter.addItem(group3);
		mAdapter.addItem(group4);
		mAdapter.addItem(group5);

		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(mItemClickListener);
		
		return view;
	}

	OnItemClickListener mItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
			Group currentItem = (Group) mAdapter.getItem(position);
			String groupName = currentItem.getGroupName();
			//Toast.makeText(getActivity(), "Selected : " + groupName, Toast.LENGTH_SHORT).show();
			
			Intent intent = new Intent(getActivity(), GroupPollListActivity.class);
			intent.putExtra("group.name", groupName);
			getActivity().startActivity(intent);
		}
	};

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.fragment_group_list, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		Log.v(TAG, "Selected option item = " + item.getTitle());

		switch (item.getItemId()) {
		case R.id.menu_new_poll:
			//Toast.makeText(getActivity(), "Selected: New poll", Toast.LENGTH_SHORT).show();
			Intent intentPollCreate = new Intent(getActivity(), PollCreateActivity.class);
			getActivity().startActivity(intentPollCreate);
			break;
			
		case R.id.menu_my_polls_onwork:
			Toast.makeText(getActivity(), "Selected: My polls on work", Toast.LENGTH_SHORT).show();
			break;
			
		case R.id.menu_my_open_polls:
			Toast.makeText(getActivity(), "Selected: My open polls", Toast.LENGTH_SHORT).show();
			break;

		case R.id.menu_settings:
			//Toast.makeText(getActivity(), "Selected: Settings", Toast.LENGTH_SHORT).show();
			Intent intentSettings = new Intent(getActivity(), SettingsActivity.class);
			getActivity().startActivity(intentSettings);
			break;

		case R.id.menu_edit_group:
			Toast.makeText(getActivity(), "Selected: Edit group", Toast.LENGTH_SHORT).show();

			for (int i = 0; i < mListView.getChildCount(); i++) {
				View itemView = mListView.getChildAt(i);
				if (itemView != null) {
					View btnEdit = itemView.findViewById(R.id.button_edit);
					btnEdit.setVisibility(View.VISIBLE);

					View buttonExit = itemView.findViewById(R.id.button_exit);
					buttonExit.setVisibility(View.VISIBLE);
					
					View badge = itemView.findViewById(R.id.new_poll_count);
					badge.setVisibility(View.INVISIBLE);
				}
			}
			break;
		}

		return true;
	}

}
